<template>
  <div class="UserManagementList">
    <el-card class="searchBox" shadow="hover">
      <div class="one">
        <el-input placeholder="请输入" v-model="inputVal">
          <el-select v-model="select" slot="prepend" @change="change">
            <el-option label="ID" value="1"></el-option>
            <el-option label="手机号" value="2"></el-option>
          </el-select>
        </el-input>

        <el-date-picker
          v-model="userTimeVal"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          @change="focus"
        >
        </el-date-picker>
        <el-button class="btn" @click="userListBtn">搜索查询</el-button>
      </div>
      <br />
      <el-radio-group v-model="system" fill="#39383D" @change="userListBtn">
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button label="ios">IOS</el-radio-button>
        <el-radio-button label="android">Android</el-radio-button>
      </el-radio-group>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="avatar" label="头像" width="80">
          <template slot-scope="scope">
            <!-- <img
              :src="scope.row.avatar"
              alt
              preview
              v-if="scope.row.avatar != ''"
              class="img"
            /> -->
            <el-image
              class="img"
              v-if="scope.row.avatar != ''"
              style="width: 70px; height: 70px"
              :src="scope.row.avatar"
              :preview-src-list="imgArr(tableData)"
            >
            </el-image>
            <img
              src="https://ui-avatars.com/api/?name=无头像&length=3&font-size=0.23"
              alt
              v-else
              class="img"
            />
          </template>
        </el-table-column>
        <el-table-column prop="id" label="用户ID"> </el-table-column>
        <el-table-column prop="name" label="用户昵称" width="160">
        </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>

        <el-table-column prop="registrationTime" label="注册时间" width="160">
        </el-table-column>
        <el-table-column prop="loginTime" label="最后登录时间" width="160">
        </el-table-column>

        <el-table-column prop="sex" label="性别"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              scope.row.state == 1
                ? "正常"
                : scope.row.state == 0
                ? "禁封"
                : "注销"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="platform" label="系统"> </el-table-column>
        <el-table-column prop="useVersion" label="版本"> </el-table-column>
        <el-table-column prop="channel" label="渠道"> </el-table-column>
        <el-table-column prop="" label="用户详情">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              @click="detailsBtn(scope.row)"
              round
              >查看详情</el-button
            >
          </template>
        </el-table-column>

        <!-- <el-table-column prop="address" label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="small"
              @click="userBanBtn(scope.row)"
              round
              v-if="scope.row.state == 0"
              >封禁用户</el-button
            >
            <el-button
              size="small"
              type="success"
              @click="unsealBtn(scope.row)"
              round
              v-if="scope.row.state == 1"
              >解封用户</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { userList, userBan } from "@/api/UserManagement";
import Pagination from "@/components/Pagination"; // 分页

export default {
  components: { Pagination },
  inject: ["reload"],
  data() {
    return {
      select: "1",
      inputVal: "",
      mobile: "",
      id: "", //ID
      system: "",
      pickerOptions: {
        disabledDate(platformValue) {
          return platformValue.getTime() > Date.now() - 8.64e6;
        },
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      userTimeValStartDate: "",
      userTimeValEndDate: "",
      userTimeVal: "", //时间日期

      tableData: [],
      total: 0,
      page: 1, //页码
      pageSize: 10, //每页条数
    };
  },
  methods: {
    //用户列表
    userList() {
      userList({
        page: this.page,
        pageSize: this.pageSize,
        startTime: "",
        endTime: "",
        id: "",
        name: "",
        phone: "",
        type: "",
      }).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.total = parseInt(res.data.total);
        }
      });
    },
    //用户列表查询
    userListBtn() {
      if (this.page !== 1) {
        this.page = 1;
      }
      userList({
        page: this.page,
        pageSize: this.pageSize,
        startTime: this.userTimeValStartDate,
        endTime: this.userTimeValEndDate,
        id: this.id,
        name: this.name,
        system: this.system,
        mobile: this.mobile,
        type: this.userOptionVal,
      }).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.total = parseInt(res.data.total);
        }
      });
    },
    //详情
    detailsBtn(row) {
      this.$router.push({
        path: "/UserManagement/UserDetailsList",
        query: {
          id: row.id,
        },
      });
    },
    // //封禁
    // userBanBtn(row) {
    //   this.$confirm("此操作将封禁该用户, 是否继续?", "封禁提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       userBan({
    //         id: row.id,
    //         ban: 1,
    //       }).then((res) => {
    //         if (res.code == 0) {
    //           this.$message({
    //             message: "封禁成功",
    //             type: "success",
    //           });
    //           this.reload();
    //         }
    //       });
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: "info",
    //         message: "已取消封禁",
    //       });
    //     });
    // },
    // //解封
    // unsealBtn(row) {
    //   this.$confirm("此操作将解封该用户, 是否继续?", "解封提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       userBan({
    //         id: row.id,
    //         ban: 0,
    //       }).then((res) => {
    //         if (res.code == 0) {
    //           this.$message({
    //             message: "解封成功",
    //             type: "success",
    //           });
    //           this.reload();
    //         }
    //       });
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: "info",
    //         message: "已取消解封",
    //       });
    //     });
    // },
    change() {
      this.inputVal = "";
      this.id = "";
      this.mobile = "";
    },
    getList() {
      userList({
        system: this.system,
        page: this.page,
        pageSize: this.pageSize,
        startTime: this.userTimeValStartDate,
        endTime: this.userTimeValEndDate,
        id: this.id,
        name: this.name,
        mobile: this.mobile,
        type: this.userOptionVal,
      }).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.total = parseInt(res.data.total);
        }
      });
    },
    //时间
    focus(time) {
      if (!this.userTimeVal) {
        this.userTimeVal = [];
      }
      this.userTimeValStartDate = this.userTimeVal[0];
      this.userTimeValEndDate = this.userTimeVal[1];
    },
  },
  created() {
    this.userList();
  },
  watch: {
    inputVal: {
      immediate: true,
      handler(val) {
        if (this.select == 1) {
          this.id = val;
        }
        if (this.select == 2) {
          this.mobile = val;
        }
      },
    },
  },
  computed: {
    imgArr() {
      return (arr) => {
        let newArr = [];
        arr.forEach((item) => {
          newArr.push(item.avatar);
        });
        return newArr;
      };
    },
  },
};
</script>

<style lang="less" scoped>
.UserManagementList {
  margin: 30px;
  height: 100%;
  .searchBox {
    .one {
      display: flex;
      .el-input {
        width: 350px;
        margin-right: 20px;
      }
      .el-button {
        margin-left: 20px;
      }
    }
  }
  .el-table {
    margin-top: 30px;
    .img {
      border-radius: 10px;
    }
  }
  .el-pagination {
    margin: 0px;
  }
  .box {
    display: flex;
    justify-content: space-between;
    margin-bottom: 40px;
    .el-input {
      width: 300px;
      .el-input-group__prepend {
        width: 110px;
      }
    }
  }
  /deep/ .el-input-group__prepend {
    width: 100px;
  }
}
</style>